package com.sendemail.thread;

import com.alibaba.fastjson.JSONObject;
import com.sendemail.config.Constants;
import com.sendemail.email.EmailModel;
import com.sendemail.service.EmailService;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import java.io.IOException;

/**
 * Created by Studio on 2017/5/15.
 * 发送邮件线程
 */
@Component
public class EmailThread extends Thread{

    private static final Logger logger = LoggerFactory.getLogger(EmailThread.class);

    @Autowired
    private EmailService emailService;
    @Autowired
    private JedisPool jedisPool;

    private EmailModel exportModel;

    private static EmailThread emailThread;
    private Jedis redis ;//= new Jedis(Constants.redisHost, Constants.redisPort,Constants.redisTimeOut);

    public EmailThread(){
    }

    @PostConstruct     //关键二   通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
    public void init() {
        emailThread = this;
        emailThread.emailService = this.emailService;
        emailThread.jedisPool = this.jedisPool;
    }

    @Override
    public void run() {
        redis = emailThread.jedisPool.getResource();
        logger.info("/********************Start loading email queue/开始读取邮件队列*****************************/");
        while(true){
            try {
                if(redis.exists(Constants.emailQueue)) {
                    String result = redis.rpop(Constants.emailQueue);
//                    logger.info(result);
                    logger.info(result);
                    exportModel = getModel(result);
                    if(exportModel.getType() == 1){
                        logger.info("Start sending an email/开始发送邮件");
                        emailThread.emailService.sendTemplateMail(exportModel);
                    }
//                    emailThread.emailService.sendTemplateMail();
                    String counter = exportModel.getCounter();
                    logger.info(counter);
                    if (redis.exists(counter)) {
                        int count = Integer.parseInt(redis.get(counter));
                        logger.info(counter + " counter/计数器" + count);
                        if (count > 0) {
                            count--;
                            redis.set(counter, count + "");
                            logger.info(counter + " counter/计数器" + count);
                        }
                    } else {
                        logger.info(" The counter does not exist/计数器不存在 ");
                    }

                    logger.info("Send complete/发送完成");
                    logger.info("/********************One export completion/一次导出完成*****************************/");
                }else{
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            } /*catch (TemplateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }*/ catch (TemplateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static EmailModel getModel(String jsonStr){

        EmailModel exportModel = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), EmailModel.class);
        return exportModel;

    }

    public EmailModel getExportModel(){
        return exportModel;
    }
}
